/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.CashBalanceDAO;
import com.ivanbiz.dao.JurnalDAO;
import com.ivanbiz.dao.ReconDAO;
import com.ivanbiz.dao.impl.CashBalanceDAOImpl;
import com.ivanbiz.dao.impl.ReconDAOImpl;
import com.ivanbiz.model.CashBalance;
import com.ivanbiz.model.GLAccount;
import com.ivanbiz.model.Jurnal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

/**
 *
 * @author IW20149X
 */
public class LaporanKeuntunganKrediturViewService extends SelectorComposer<Component> {

    CashBalanceDAO cashBalanceDAO;
    JurnalDAO jurnalDAO;
    SimpleDateFormat dateFormat;
    NumberFormat numberFormat;
    @Wire
    Label label;
    @Wire
    Listbox listJurnal;
    @Wire
    Textbox totalDebet;
    @Wire
    Textbox totalKredit;
    @Wire
    Textbox totalKeuntungan;
    @Wire
    Textbox cash;
    @Wire
    Textbox jumlah;
    ReconDAO reconDAO;
    GLAccount gLAccount;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        listJurnal.getItems().clear();
        gLAccount = (GLAccount) Executions.getCurrent().getArg().get("gLAccount");
        Date dari = (Date) Executions.getCurrent().getArg().get("dari");
        Date sampai = (Date) Executions.getCurrent().getArg().get("sampai");

        dateFormat = new SimpleDateFormat("dd-MMMM-yyyy");
        numberFormat = NumberFormat.getNumberInstance();
        cashBalanceDAO = new CashBalanceDAOImpl();
        CashBalance cashBalance = cashBalanceDAO.getBalanceByOrderDate(gLAccount.getId());
        label.setValue("Dari tanggal " + dateFormat.format(dari) + " sampai tanggal " + dateFormat.format(sampai));

        reconDAO = new ReconDAOImpl();
        List<Jurnal> loadJurnal = reconDAO.getDataByDateAndGLAccount(dateFormat.format(dari), dateFormat.format(sampai), gLAccount.getNoGL());

        int no = 0;
        int x = 0;
        double totalDebets = 0;
        double totalKredits = 0;
        double totalKeuntungans;
        double jumlahs;
        for (Jurnal jurnal : loadJurnal) {
            no += 1;
            Listitem item = new Listitem();
            item.setValue(jurnal);
            item.appendChild(new Listcell(String.valueOf(no)));
            item.appendChild(new Listcell(dateFormat.format(jurnal.getDateReference())));
            item.appendChild(new Listcell(jurnal.getGLAccount()));
            item.appendChild(new Listcell(jurnal.getCurrency()));
            item.appendChild(new Listcell(numberFormat.format(jurnal.getDebit())));
            item.appendChild(new Listcell(numberFormat.format(jurnal.getCredit())));
            totalDebets += jurnal.getDebit();
            totalKredits += jurnal.getCredit();
            x++;
        }
        totalKredit.setValue(numberFormat.format(totalKredits));
        totalDebet.setValue(numberFormat.format(totalDebets));
        totalKeuntungans = totalKredits - totalDebets;
        totalKeuntungan.setValue(numberFormat.format(totalKeuntungans));
        cash.setValue(numberFormat.format(cashBalance == null ? new Double("0") : cashBalance.getBalance()));
        jumlahs = totalKeuntungans + (cashBalance == null ? new Double("0") : cashBalance.getBalance());
        jumlah.setValue(String.valueOf(new Double(jumlahs).intValue()));
    }

    @Listen("onClick=#tutup;")
    public void tutup() {
        Messagebox.show("Apakah Tutup Cash Balance ingin diproses ?", "Confirm Dialog", Messagebox.OK | Messagebox.CANCEL, Messagebox.QUESTION, new org.zkoss.zk.ui.event.EventListener() {

            public void onEvent(Event evt) throws InterruptedException {
                if (evt.getName().equals("onOK")) {
                    try {
                        CashBalance cashBalanceSave = new CashBalance();
                        cashBalanceSave.setGlAccount(gLAccount);
                        cashBalanceSave.setDateBalance(new Date());
                        cashBalanceSave.setBalance(jumlah.getValue().isEmpty() ? (double) 0 : new Double(jumlah.getValue()));
                        cashBalanceSave.setStatus("0");
                        if (cashBalanceDAO.validateSameDate(cashBalanceSave.getGlAccount().getId(), cashBalanceSave.getDateBalance())) {
                            Messagebox.show("Close Balance suda dilakukan untuk tanggal " + dateFormat.format(cashBalanceSave.getDateBalance()));
                        } else {
                            cashBalanceDAO.saveOrUpdate(cashBalanceSave);
                            Messagebox.show("Cash Balance sudah ditutup");
                        }
                    } catch (Exception ex) {
                        Logger.getLogger(LaporanKeuntunganKrediturViewService.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });
    }
}
